﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taobao.common;

namespace Taobao.Saver
{
    public delegate void ShopSaveCompletedEventHandler(object sender,EventArgs e);
    public partial class SaveItem : IDisposable
    {
        public SaveItem()
        #region >><<
        {
            initData(this);
            initThread();
        }

        public SaveItem(string section)
            : this()
        {
            Section = section;
        }

        public bool isDispose { get; set; }
        public void Dispose()
        {
            isDispose = true;
            disThread();
        }
        #endregion

        public string Section { get; set; }

        public ShopSaveCompletedEventHandler ShopSaveCompletedEvent { get; set; }
        protected virtual void OnShopSaveCompleted()
        {
            _CurrentItem = null; 

            if (ShopSaveCompletedEvent != null)
                ShopSaveCompletedEvent(this, EventArgs.Empty);
        }

        public SHOPArgs CurrentItem
        #region >><<
        {
            get { return _CurrentItem; }
            set
            {
                initThread();
                _CurrentItem = value;
                _OddsAddEvent.Set();
            }
        }
        private SHOPArgs _CurrentItem = null;
        #endregion

        #region >>ItemAdd<<
        private System.Threading.Thread OddsAddThread;
        private System.Threading.ManualResetEvent _OddsAddEvent;

        private void initThread()
        #region >><<
        {
            if (_OddsAddEvent == null)
                _OddsAddEvent = new System.Threading.ManualResetEvent(false);

            if (OddsAddThread != null && OddsAddThread.IsAlive) return;
            OddsAddThread = new System.Threading.Thread(new System.Threading.ThreadStart(WebsiteOddsItemAdd));
            OddsAddThread.IsBackground = true;
            OddsAddThread.Start();
        }

        private void disThread()
        {
            if (_OddsAddEvent != null)
                _OddsAddEvent.Close();
            _OddsAddEvent = null;

            if (OddsAddThread != null && !OddsAddThread.IsAlive)
            {
                try
                {
                    OddsAddThread.Abort();
                }
                catch (System.Threading.ThreadAbortException) { }
            }
        }
        #endregion

        private void WebsiteOddsItemAdd()
        {
            try
            {
                while (!isDispose)
                {
                    if (_OddsAddEvent == null) break;
                    _OddsAddEvent.WaitOne();
                    if (CurrentItem != null && CurrentItem.Shop != null)
                        CurrentItem = null;
                    if (CurrentItem == null)
                        _OddsAddEvent.Reset();
                    else
                    #region >><<
                    {
                        string name = CurrentItem.Shop.Name;
                        WriteLine("[" + name + "]  Running...");
                        //獲取店鋪資料

                        SShop item1;
                        if (ShopManager.Instance.getITEM(CurrentItem.Shop.ID, out item1))
                        #region >><<
                        {//Update
                            if (!Compare(CurrentItem.Shop, item1.Shop))
                            {
                                if (ShopManager.Instance.Update(CurrentItem.Shop, item1))
                                    WriteLine("[" + name + "]  Update Success...");
                                else
                                {
                                    WriteLine("[" + name + "]  Update fail...");
                                    item1 = null;
                                }
                            }
                        }
                        else
                        {//Add
                            if (ShopManager.Instance.Add(CurrentItem.Shop, out item1))
                                WriteLine("[" + name + "]  Add Success...");
                            else
                            {
                                WriteLine("[" + name + "]  Add fail...");
                                item1 = null;
                            }
                        }
                        #endregion

                        if (item1 == null)
                        {
                            OnShopSaveCompleted();
                            continue;
                        }

                        if (CurrentItem.Products != null && CurrentItem.Products.Count > 0)
                        {


                        }

                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                WriteLine(ex.Message);
                CurrentItem = null;
            }
        }
        #endregion

       

        public static int RunSQLCount { get { return _RunSQLCount; } set { _RunSQLCount = value; } }
        private static int _RunSQLCount = 10;

        public static int MaxCount { get { return _MaxCount; } set { _MaxCount = value; } }
        private static int _MaxCount = 100;
        private static object _lock = new object();
        public static bool Next(string section, out SaveItem item)
        {
            if (Data == null)
                Data = new List<SaveItem>();
            item = Data.Find(r => r.CurrentItem == null && r.Section == section);
            if (item == null && Data.Count < MaxCount)
            {
                item = new SaveItem(section);
                Data.Add(item);
            }

            Console.WriteLine("[Next] " + section + "," + (item != null).ToString() + ((item != null) ? " -------------------" : ""));
            return item != null;
        }



        #region >>WriteLine<<
        protected static void WriteLine(string msg)
        {
            Console.WriteLine(msg);
        }

        public static void WriteLine(string n2, string msg)
        {
            Console.WriteLine("[" + n2 + "]" + msg);
        }

        public static void WriteLine(string N1, string n2, string msg)
        {
            Console.WriteLine("[" + N1 + ":" + n2 + "]" + msg);
        }

        protected static void Write(string msg)
        {
            Console.Write(msg);
        }
        #endregion
    

        public static List<SaveItem> Data { get; set; }
        private static void initData(SaveItem item)
        {
            if (Data == null)
                Data = new List<SaveItem>();
            Data.Add(item);
        }

    }
}
